package com.example.toy.core.utils;

import com.example.toy.core.version.ToyVersion;
import org.apache.hadoop.fs.Path;

/**
 * VersionHelper
 *
 * @author shenbing
 * @date 2021/12/20
 */
public class VersionHelper {

  public static ToyVersion fromPath(Path path) {
    String name = path.getName();
    if (!isToyFile(name)) {
      throw new IllegalArgumentException("Can not get version from file: " + path);
    }
    int dotOfExtension = name.lastIndexOf(".");
    dotOfExtension = dotOfExtension < 0 ? dotOfExtension = name.length() : dotOfExtension;
    String num = name.substring("toy".length(), dotOfExtension);
    return new ToyVersion(Long.parseLong(num));
  }

  public static boolean isToyFile(String fileName) {
    return fileName.startsWith("toy");
  }

  public static String getExtension(String fileName) {
    int i = fileName.lastIndexOf(".");
    if (i < 0) {
      return "";
    } else {
      return fileName.substring(i + 1);
    }
  }
}
